﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace wap主题
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form3 f=new Form3();
            if(f.ShowDialog()==DialogResult.OK)
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("select * from 主题");
                DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
                dataGridView1.DataSource = dt;
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                int row=dataGridView1.SelectedRows[0].Index;
                Form3 f = new Form3(主题DataFactory.Construct(dataGridView1,row));
                if (f.ShowDialog() == DialogResult.OK)
                {
                    StringBuilder sql = new StringBuilder();
                    sql.Append("select * from 主题");
                    DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
                    dataGridView1.DataSource = dt;
                }
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 主题");
            DataTable dt=WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                int row = dataGridView1.SelectedRows[0].Index;
                Form3 f = new Form3(主题DataFactory.Construct(dataGridView1, row));
                if (f.ShowDialog() == DialogResult.OK)
                {
                    StringBuilder sql = new StringBuilder();
                    sql.Append("select * from 主题");
                    DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
                    dataGridView1.DataSource = dt;
                }
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                if (MessageBox.Show("是否删除当前选中任务，删除后无法恢复", "警告", MessageBoxButtons.YesNo) == DialogResult.No)
                    return;
                int row = dataGridView1.SelectedRows[0].Index;
                主题Data d=主题DataFactory.Construct(dataGridView1, row);
                string sql = "DELETE FROM 主题 WHERE  (名称 = '" + d.名称 + "')";
                WFNetLib.ADO.AccessOP.NonQuerySQL(sql);
                MessageBox.Show("删除成功");
                sql="select * from 主题";
                DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
                dataGridView1.DataSource = dt;
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否删除所有任务，删除后无法恢复", "警告", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
            string sql = "DELETE FROM 主题";
            WFNetLib.ADO.AccessOP.NonQuerySQL(sql);
            MessageBox.Show("删除成功");
            sql = "select * from 主题";
            DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
            dataGridView1.DataSource = dt;
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            string strResult;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream fStream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                StreamReader sReader = new StreamReader(fStream, Encoding.Default);
                StringBuilder no = new StringBuilder();
                while (!sReader.EndOfStream)
                {
                    strResult = sReader.ReadLine();
                    strResult = strResult.Substring(5);
                    string[] s = strResult.Split(new string[]{"||"},StringSplitOptions.None);
                    StringBuilder sql = new StringBuilder();
                    sql.Append("SELECT 名称 FROM 主题 WHERE (名称 = '");
                    sql.Append(s[0]);
                    sql.Append("'");
                    sql.Append(")");
                    DataTable dt = WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
                    if (dt.Rows.Count != 0)
                    {
                        no.Append(strResult);
                        no.Append("\r\n");
                        continue;
                    }
                    sql.Clear();
                    sql.Append("INSERT INTO 主题 (名称, 作品名, 版权归属及分成模式, 推荐指数, 资费, 简单说明, wap链接地址, ua) VALUES   (");
                    sql.Append("'");
                    sql.Append(s[1]);
                    sql.Append("',");
                    sql.Append("'");
                    sql.Append(s[2]);
                    sql.Append("',");
                    sql.Append("'");
                    sql.Append(s[3]);
                    sql.Append("',");
                    sql.Append("'");
                    sql.Append(s[4]);
                    sql.Append("',");
                    sql.Append("'");
                    sql.Append(s[5]);
                    sql.Append("',");
                    sql.Append("'");
                    sql.Append(s[6]);
                    sql.Append("',");
                    sql.Append("'");
                    sql.Append(s[7]);
                    sql.Append("',");
                    sql.Append("'");
                    sql.Append(s[8]);
                    sql.Append("'");
                    sql.Append(")");
                    if (WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString()) != 1)
                    {
                        no.Append(strResult);
                        no.Append("\r\n");
                    }                    
                }
                if (no.Length != 0)
                    MessageBox.Show("不成功的导入有如下条目:\r\n" + no.ToString());               
                DataTable dt1 = WFNetLib.ADO.AccessOP.DataTableSQL("select * from 主题");
                dataGridView1.DataSource = dt1;
            }
        }
    }
}
